	var fieldJobId;
	var changeFromClick = false;
	//var reBackNodeText;
	
	//定位显示
	var arrayLi = [];
	var arrayLiInd = 0;
	
	var resultSysNum = 0;
	var loadSysNum = 0;
	$(document).ready(function(){
		$("#ff input").val("");
		$("#ff :input").bind('change',function() {
			$("#ff").data("changed",true);
		});
		$("#txtCreateDateTime").datebox({
			onSelect:function(record)
			{
				$("#ff").data("changed",true);
			}
		});  

		$("#datagrid").datagrid({
			url : BASE_URL_ADDRESS_MAPPING_SEARCH + '/findByExample' + '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,
			nowrap:false,
			pageSize:20,
			singleSelect:true,
			striped:true,
			pageList:[10,20,50,100],
			columns:[[
						{field:'fieldName',title:'领域',width:100},
						{field:'fieldSystemName',title:'系统',width:100},
						{field:'name',title:'接口名称',width:100},
						{field:'priUrl',title:'原始地址',width:300,align:'left'},
						{field:'pubUrl',title:'逻辑地址',width:300,align:'left'}
			      ]],
			pagination:true,
			onSelect:function(rowIndex,rowData){
				$("#toolbar_edit").linkbutton('enable');
				$("#toolbar_delete").linkbutton('enable');
			},
			loadFilter:function(data){
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				
				param.sort = 'createTime,desc';
				
			}
		});
		
		
		
		
	    //领域系统结构树
		var tree_id = '';
		var isSave = false;
		var modifyNode;
		//var reBackNode;
		
		$("#tt").tree(
				{
					url : BASE_URL_TREE
						+ '/getHttp4User4EasyUiTree'
						+ '?v=' + Math.random(),
					lines : false,
					onLoadSuccess : function() {
						$(
								"#tt > li > ul > li > .tree-node > .tree-file, #tt > li > .tree-node > .tree-file")
								.css("background",
										"url('../../../images/st_folder.png') 2px 2px no-repeat");
						
						var nodes = $('#tt').tree('getChildren');
						function changeNodeClass(nodelist, start, end) {

							if(start < nodelist.length)
							{
								for(var i = start; i <= end; i++)
								{
									var node = nodelist[i];
									if(node.nodeType == "fieldSystem" && node.hasChildren && (!node.children || node.children.length == 0) && $(node.target).find(".tree-fieldSys").length == 0 )
									{
										$(node.target).find("span.tree-icon").prev().remove();
										$(node.target).children(".tree-icon.tree-file").before('<span class="tree-fieldSys tree-collapsed" data-id="' + node.id + '"></span>');
										$(node.target).children(".tree-fieldSys").click(function(){

											var $self = $(this);
											if($self.hasClass('tree-collapsed'))
											{//alert(node.id+'  '+node.text);													
												$self.removeClass('tree-collapsed').addClass('tree-expanded');
												//$('#tt').tree('expend', $self.parent()[0])
												$.ajax({
													url :BASE_URL_TREE+ '/getHttpBySystemId?systemId=' + $self.attr("data-id") + '&operationType=1',
													dataType : 'json',
													type : 'get',
													success : function(data){
														//alert(data)
														$('#tt').tree('append',{
															parent: $self.parent()[0],
															data: data
														});
													}
												}); 
											}
											else
											{
												$self.addClass('tree-collapsed').removeClass('tree-expanded');
												$('#tt').tree('collapse', $self.parent()[0]);
											}
											
										});
									}
									var icons = $(node.target).find("span.tree-icon");
									if (node.nodeType == 'systemInterface') {
										//var icons = $(node.target).find("span.tree-icon");
										if(node.interfaceType=='1'){
											$(icons[0]).removeClass("tree-file");
											$(icons[0]).addClass("icons4WebService");
										}
										else if(node.interfaceType=='2'){
											$(icons[0]).removeClass("tree-file");
											$(icons[0]).addClass("icons4Rest");
										}
										else if(node.interfaceType=='3'){
											$(icons[0]).removeClass("tree-file");
											$(icons[0]).addClass("icons4Ftp");
										}
									}
								}
								if(end + 1 < nodelist.length)
								{
									start = end + 1;
									end += 100;
									end = end > nodelist.length - 1? nodelist.length - 1:end;
									setTimeout(function(){
										changeNodeClass(nodelist, start, end);
									}, 50);
								}
							}
						}
						var end = nodes.length > 100? 99:nodes.length;
						changeNodeClass(nodes, 0, end);
					},
					onBeforeSelect : function(node) {
						if ($(node.target).children(
								".tree-folder").length) {
							return false;
						}
						if ($(node.target).children(
								".tree-file").length
								&& ($(node.target).parent()
										.parent().parent()
										.parent()[0].id == "tt" || $(
										node.target)
										.parent().parent()[0].id == "tt")) {
							return false;
						}
						return true;
					},
					onClick : function(node) {
						arrayLi = [];
						arrayLiInd = 0;
						changeFromClick = true;
						$("#systemName").val(node.text);
						if(node.nodeType=="field"){
							$("#datagrid").datagrid("reload",  BASE_URL_ADDRESS_MAPPING_SEARCH + '/findByExample?fieldName='+node.id+ '&v=' + Math.random());
						}
						if(node.nodeType=="fieldSystem"){
							$("#datagrid").datagrid("reload",  BASE_URL_ADDRESS_MAPPING_SEARCH + '/findByExample?fieldSystemName='+node.id +'&v=' + Math.random());
						}
						if(node.nodeType=="systemInterface"){
							$("#datagrid").datagrid("reload",  BASE_URL_ADDRESS_MAPPING_SEARCH + '/findByExample?id='+node.id+ '&v=' + Math.random());
						}
					},
				});
		 
		
	});
	
	$("#searchNode").click(function (e) { 
		if($("#systemName").val() != '')
		{
			//后台查询匹配的接口
			$.ajax({
				url : BASE_URL_HTTP_INTERFACE + '/findHttpInterfaceByIfName' + '?ifName=' + encodeURI($("#systemName").val()) + '&v=' + Math.random(),
				dataType : 'json',
				type : 'get',
				success : function(data) {
					if(data.backValue == true){
						var system = data.httpDatas;
						if(system.indexOf(",") != -1){
							var systems = system.split(",");
						}else{
							var systems = [system];
						}
						resultSysNum = systems.length;
						loadSysNum = 0;
						for(var i=0; i < systems.length; i++){
							//展开系统的节点
							//找到树的所有节点
							var nodes = $('#tt').tree('getChildren');
							for(var j = 0; j <= nodes.length; j++)
							{
								if(nodes[j].text == systems[i]){
									var $self = $(nodes[j].target).find("span.tree-fieldSys");
									if($self.length)
									{	
										 expendNode(nodes[j]);
									}
									else
									{
										loadSysNum++;
										if(loadSysNum >= resultSysNum)
										{
											choose_tree();
										}
									}
									break;
								}
							}
							
						}
					
					}else{
						$.messager.alert("错误","<p>没有找到包含  " + $("#systemName").val() + " 的接口！ </p>","error");
					}
				},
				error : function() {
					$.messager.alert("错误","<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>","error");
				}
			});
		}else
		{
			//搜索关键字为空时，不进行搜索
			/*$("input").val("");
			var row = $('#tt li .tree-node');  
			row.removeClass('tree-node-selected');*/
			$(".tree_con").scrollTop(0);
		}
	});  
	$("#systemName").bind('input propertychange', function() {
		if(changeFromClick) {
			changeFromClick = false;
			return;
		}
		arrayLi = [];
		arrayLiInd = 0;
		resultSysNum = 0;
		loadSysNum = 0;
		if($("#systemName").val() != '')
		{
			//choose_tree();
		}else
		{  //搜索关键字为空时，不进行搜索
			/*$("#ff input").val("");
			var row = $('#tt li .tree-node');  
			row.removeClass('tree-node-selected');*/
			$(".tree_con").scrollTop(0);
		}
	});
	
	function expendNode(node) {
		var $self = $(node.target).find("span.tree-collapsed");
		if(!$self.length)
		{
			return;
		}
		$self.removeClass('tree-collapsed').addClass('tree-expanded');
		$.ajax({
			url :BASE_URL_TREE+ '/getHttpBySystemIdForScan?systemId=' + node.id,
			dataType : 'json',
			type : 'get',
			success : function(data){
				//alert(data)
				$('#tt').tree('append',{
					parent: $self.parent()[0],
					data: data
				});
				loadSysNum++;
				if(loadSysNum >= resultSysNum)
				{
					choose_tree();
				}
			},
			error: function() {
				loadSysNum++;
				if(loadSysNum >= resultSysNum)
				{
					choose_tree();
				}
			}
		}); 
	}
	$(document , "#systemName").keyup(function(event){
	  if(event.keyCode ==13){
	    $("#searchNode").trigger("click");
	  }
	});
	
	function getSelected()
	{
		var node = $('#tt').tree('getSelected');
		if(node.nodeType == 'systemInterface'){
			$.ajax({
				url: BASE_URL_BASE_URL_SYSTEM_INTERFACE_CRUD +'/' + node.id + '?v=' + Math.random(),
				dataType:'json',
				type:'get',
				contentType:"application/json",
				success:function(data){
					FormEcho(data);
				},
				error:function(){
					/*alert("error");*/
				}
				
				
			});
		}
	}
	
	function choose_tree()
	{
		$('#tt').tree('expandAll');
		if(arrayLi.length == 0 && arrayLiInd == 0)
		{
			var searchVal = $("#systemName").val(); 
			var row = $('#tt li .tree-node');  
			var rowcolumn = $('#tt li .tree-node > .tree-title');  
			var i = 0, j = 0;
			
			//row.removeClass('tree-node-selected');
			row.removeClass('tree-node-hover'); 
			rowcolumn.each(function (column) {  
				var matchVal = $(this).text(); 
				if(matchVal.indexOf(searchVal) != -1)
				{
					i++;
					var scrTop = $(".tree_con").scrollTop() + $(this).offset().top - 
						parseInt($(".tree_con").css("padding-top"),10) - 
						$(".search-con")[0].offsetHeight - $(".left_menu > .menu_tit")[0].offsetHeight;

					if(i <= 1)
					{
						//$(this).parent().addClass('tree-node-selected'); 
						$(this).parent().addClass('tree-node-hover'); //改成鼠标hover状态，不选中搜索结果

						$(".tree_con").scrollTop(scrTop);
						arrayLiInd = 1;
						//getSelected();
					}				
					arrayLi.push([matchVal,scrTop]);
				}
				}); 
			}else
			{
				var rowcolumn = $('#tt li .tree-node > .tree-title'); 
				var row = $('#tt li .tree-node');  
				//row.removeClass('tree-node-selected');
				row.removeClass('tree-node-hover'); 
				if(arrayLiInd <  arrayLi.length)
				{
					rowcolumn.each(function (column) {  
						var matchVal = $(this).text(); 
						if(matchVal == arrayLi[arrayLiInd][0])
						{
							//$(this).parent().addClass('tree-node-selected'); 
							$(this).parent().addClass('tree-node-hover'); //改成鼠标hover状态，不选中搜索结果
							$(".tree_con").scrollTop(arrayLi[arrayLiInd][1]);
							arrayLiInd++;
							//getSelected();
							return false;
						}
					});
				}else
				{
					//arrayLi = [];
					arrayLiInd = 0;
					$(".tree_con").scrollTop(0);
					choose_tree();
				}
			}
	}
	//表单数据回显
	function FormEcho(data){
		$("#txtJobName").val(data.jobName);
		$("#comboxTechnicalStandard").combobox('setValue',data.tecStandard);
		$("#comboxInterfaceSenderField").combobox('setValue',data.senderFieldSystem_fieldId);
		$("#comboxInterfaceReceiverField").combobox('setValue',data.receiverFieldSystem_fieldId);
		$("#comboxInterfaceSender").combobox('setValue',data.senderFieldSystemId);
		$("#comboxInterfaceReceiver").combobox('setValue',data.receiverFieldSystemId);
		$("#comboxDatabaseTypeSender").combobox('setValue',data.senderDBType);
		$("#comboxDatabaseTypeReceiver").combobox('setValue',data.receiverDBType);
		$("#comboxInterfaceScan").combobox('setValue',data.interfaceScene);
		$("#txtCreateDateTime").datebox('setValue',data.createTime);
		$("#txtAreaInterfaceDescription").val(data.interfaceDesc);
		
		
		$("#comboxTechnicalStandard").siblings(".textbox").children(".textbox-text").val($("#comboxTechnicalStandard").combobox('getText'));	
		$("#comboxInterfaceSenderField").siblings(".textbox").children(".textbox-text").val($("#comboxInterfaceSenderField").combobox('getText'));
		$("#comboxInterfaceReceiverField").siblings(".textbox").children(".textbox-text").val($("#comboxInterfaceReceiverField").combobox('getText'));
		$("#comboxInterfaceSender").siblings(".textbox").children(".textbox-text").val($("#comboxInterfaceSender").combobox('getText'));
		$("#comboxInterfaceReceiver").siblings(".textbox").children(".textbox-text").val($("#comboxInterfaceReceiver").combobox('getText'));
		$("#comboxDatabaseTypeSender").siblings(".textbox").children(".textbox-text").val($("#comboxDatabaseTypeSender").combobox('getText'));
		$("#comboxDatabaseTypeReceiver").siblings(".textbox").children(".textbox-text").val($("#comboxDatabaseTypeReceiver").combobox('getText'));
		$("#comboxInterfaceScan").siblings(".textbox").children(".textbox-text").val($("#comboxInterfaceScan").combobox('getText'));
		$("#txtCreateDateTime").siblings(".textbox").children(".textbox-text").val($("#txtCreateDateTime").combobox('getText'));
	}


/*$('#txtJobName').bind('input propertychange keyup', function() {
	var node = $('#tt').tree('getSelected');
	if(!(node === null))
	{
		$("#tt").tree('update',{
			target:node.target,
			text:''+$('#txtJobName').val()+''
		});
	}
});*/
